package com.team.easybuy.service.Impl;

import com.team.easybuy.entity.Category;
import com.team.easybuy.entity.Product;
import com.team.easybuy.entity.ProductCategory;
import com.team.easybuy.entity.ProductCategory2;
import com.team.easybuy.mapper.ProductCategoryMapper;
import com.team.easybuy.mapper.ProductMapper;
import com.team.easybuy.service.ProductCategoryService;
import com.team.easybuy.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductService productService;

    @Override
    public List<ProductCategory> listProCategory() {
        return productCategoryMapper.selectAllOne();
    }

    @Override
    public List<ProductCategory2> listProCategory2(Integer id) {
        List<ProductCategory2> two = productCategoryMapper.selectAllTwo(id);
        for (ProductCategory2 productCategory2 : two) {
            productCategory2.setProductCategory3(productCategoryMapper.selectAllThree(productCategory2.getId()));
        }
        return two;
    }

    @Override
    public List<ProductCategory> listGoods() {
        List<ProductCategory> list = productCategoryMapper.selectAllOne();
        for (ProductCategory productCategory : list) {
            productCategory.setProductCategory2List(productCategoryMapper.selectAllTwo(productCategory.getId()));
            productCategory.setProductList(productService.selectByCId(productCategory.getId()));
        }
        return list;
    }

    @Override
    public List<Category> listAllCate() {
        List<Category> list = productCategoryMapper.selectAll();
        for (Category category : list) {
            Category pId = productCategoryMapper.selectByPId(category.getParentId());
            category.setParentCate(pId);
        }
        return list;
    }

    @Override
    public String deleteById(Integer id) {
        List<ProductCategory> list = productCategoryMapper.selectById(id);
        if (list.size() > 0) {
            return "此类别存在子类无法删除";
        } else {
            List<Product> products = productService.solrByCId123(id);
            if (products.size() == 0) {
                if (productCategoryMapper.deleteById(id) > 0) {
                    return "删除成功";
                } else {
                    return "删除失败";
                }
            } else {
                return "此类别下有商品无法删除";
            }
        }
    }

    @Override
    public List<ProductCategory> loadCateWhich(Integer id, Integer type) {
        return productCategoryMapper.loadCateWhich(id, type);
    }

    @Override
    public String addCate(Map<String, Object> params) {
        String name = params.get("name").toString();
        int type = Integer.parseInt(params.get("type").toString());
        int one = 0;
        int two = 0;
        if (!params.get("one").equals("")) {
            one = Integer.parseInt(params.get("one").toString());
        }
        if (!params.get("two").equals("")) {
            two = Integer.parseInt(params.get("two").toString());
        }
        Category category;
        if (type == 1) {
            category = new Category(name, type, 0);
        } else if (type == 2) {
            category = new Category(name, type, one);
        } else {
            category = new Category(name, type, two);
        }
        if (productCategoryMapper.selectByPIdAndName(category) == null) {
            if (productCategoryMapper.insertCate(category) > 0) {
                return "添加成功";
            } else {
                return "添加失败";
            }
        }
        return "名称已存在";
    }

    @Override
    public Category loadCateOne(Integer id, Integer type) {
        Category category = productCategoryMapper.selectByPId(id);
        Category parent = productCategoryMapper.selectByPId(category.getParentId());
        if (parent != null) {
            category.setParentCate(parent);
            Category grandParent = productCategoryMapper.selectByPId(parent.getParentId());
            if (grandParent != null) {
                parent.setParentCate(grandParent);
            }
        }
        return category;
    }

    @Override
    public String editCate(Map<String, Object> params) {
        String name = params.get("name").toString();
        int id = Integer.parseInt(params.get("id").toString());
        int type = Integer.parseInt(params.get("type").toString());
        int one = 0;
        int two = 0;
        if (!params.get("one").equals("")) {
            one = Integer.parseInt(params.get("one").toString());
        }
        if (!params.get("two").equals("")) {
            two = Integer.parseInt(params.get("two").toString());
        }
        Category category;
        if (type == 1) {
            category = new Category(id, name, type, 0);
        } else if (type == 2) {
            category = new Category(id, name, type, one);
        } else {
            category = new Category(id, name, type, two);
        }
        if (productCategoryMapper.selectByPIdAndName(category) == null) {
            if (productCategoryMapper.updateCate(category) > 0) {
                return "修改成功";
            } else {
                return "修改失败";
            }
        }
        System.out.println(category);
        return "名称已存在";
    }
}
